package com.beesphere.excel.writer.style.resolvers;

import java.lang.reflect.Method;

import com.beesphere.excel.writer.impls.WorkbookContext;
import com.beesphere.excel.writer.style.StyleResolver;

public abstract class MethodBasedStyleResolver implements StyleResolver {

	private static final long serialVersionUID = 304415333021865850L;
	
	protected Method method;
	
	public MethodBasedStyleResolver (Method method) {
		this.method = method;
	}
	
	@Override
	public void apply (WorkbookContext workbookContext, Object styleElement, String path, Object value) {
		if (value == null || method == null) {
			return;
		}
		try {
			Object v = getValue (workbookContext, styleElement, value);
			if (v != null) {
				method.invoke (
					styleElement, 
					new Object [] {
						v
					}
				);
			}
		} catch (Throwable e) {
			// ignore
			e.printStackTrace();
		} 
	}
	
	@Override
	public Object create (WorkbookContext workbookContext) {
		return null;
	}
	
	protected abstract Object getValue (WorkbookContext workbookContext, Object applyTo, Object value);
		
}
